package com.example.administrator.familfinalcialmanagement;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.LoginBean;
import com.example.administrator.familfinalcialmanagement.bean.User;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityMainBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.UserDao;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.ui.RegisterActivity;
import com.example.administrator.familfinalcialmanagement.ui.activity.ForgetPwdActivity;
import com.example.administrator.familfinalcialmanagement.util.ApiHTTP;
import com.example.administrator.familfinalcialmanagement.util.GsonUtil;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.PermissionUtil;
import com.example.administrator.familfinalcialmanagement.util.PhontNumberUtil;
import com.example.administrator.familfinalcialmanagement.util.SpUtils;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.example.administrator.familfinalcialmanagement.util.dialog.InCommonDialog;
import com.example.administrator.familfinalcialmanagement.util.dialog.RegisterDialog;
import com.example.administrator.familfinalcialmanagement.util.listener.OnBHClickListener;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

import static com.example.administrator.familfinalcialmanagement.Constants.LOGINPRESSION;
import static com.example.administrator.familfinalcialmanagement.Constants.REQEEST_CDOE_GPS;

public class MainActivity extends BaseActivity {

    ActivityMainBinding binding;
    List<User> list = new ArrayList<>();
    UserDao userDao = GreenDaoUtils.getSingleTon().getmDaoSession().getUserDao();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        binding = DataBindingUtil.setContentView( this, R.layout.activity_main );
        binding.setActivity( this );
        initView();
    }

    private void initView() {
        String isLogin = (String) SpUtils.get( "isLogin", "" );
        if ("1".equals( isLogin )) {
            startActivity( new Intent( MainActivity.this, GRActivity.class ) );
            finish();
        }

        //动态申请权限
        initPermission();
        binding.textViewRegister.setOnClickListener( this );
        binding.textViewMain.setOnClickListener( this );
    }

    private void initPermission() {
        if (PermissionUtil.loginPermission( this, LOGINPRESSION )) {
            if (!PermissionUtil.isOPen( MainActivity.this )) {
                Intent intent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS );
                startActivityForResult( intent, REQEEST_CDOE_GPS );
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textViewRegister:
                RegisterDialog dialog = new RegisterDialog( this );
                dialog.setTitleText( "请选择角色", "老师", "学生" );
                dialog.setOnBHClickListener( new OnBHClickListener() {
                    @Override
                    public void onConfirmClick() {
                        startActivity( new Intent( MainActivity.this, RegisterActivity.class )
                                .putExtra( "module_type", "1" ) );
                    }

                    @Override
                    public void onCloseClick() {
                        startActivity( new Intent( MainActivity.this, RegisterActivity.class )
                                .putExtra( "module_type", "2" ) );
                    }
                } );
                dialog.show();
                break;
            case R.id.textViewMain:
                String phone = binding.phone.getText().toString().trim();
                if (TextUtils.isEmpty( phone )) {
                    ToastUtil.showToast( "请输入账号" );
                    return;
                }

                String pwd = binding.pwd.getText().toString().trim();
                if (TextUtils.isEmpty( pwd )) {
                    ToastUtil.showToast( "请输入密码" );
                    return;
                }

                boolean isUser = false;
                for (User user : userDao.loadAll()) {
                    if (user.getName().equals( phone ) && user.getPwd().equals( pwd )) {
                        isUser = true;
                    } else {
                        LogUtil.e( "不相同" );
                        LogUtil.e( user.getName() + "===" + phone );
                        LogUtil.e( user.getPwd() + "===" + pwd );
                    }
                }

                if (isUser) {
                    SpUtils.put( "module_name", phone );
                    SpUtils.put( "module_pwd", pwd );
                    startActivity( new Intent( MainActivity.this, GRActivity.class ) );
                    finish();
                } else {
                    ToastUtil.showToast( "登陆失败" );
                }
                break;
            default:
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return true;
    }
}
